package com.today.cjl.sqlGen.impl

import com.today.cjl.sqlGen.utils.{FileUtil, GenUtil}

/**
  * Created by cjl at 2018-09-04
  * Description: 
  * Changed: 
  */
object GenWithIdAndArgs extends GenUtil[Map[String, Any]] {

  /**
    * id列表文件
    */
  private val ID_FILE: String = "tmp/ids.txt"

  /**
    * 生成sql  每个模板对应同一套参数  公共参数 * n
    * @param commonArgs
    */
  override def gen(commonArgs: Map[String, Any]): Unit = {

    val argsList: List[List[Map[String, Any]]] = genIds().map {
      genIds2ArgsMap(commonArgs, _)
    }
    genSql(argsList)
  }

  /**
    * 多个id列表 一组id对应一个模板
    * @return
    */
  private def genIds(): List[List[String]] = {
    val lines: List[String] = FileUtil.readLine(ID_FILE)
    lineSeq(lines)
  }

  /**
    * 组合参数列表列表
    * @param commonArgs
    * @return
    */
  private def genIds2ArgsMap(commonArgs: Map[String, Any], ids: List[String]): List[Map[String, Any]] = {
    ids.map(id => commonArgs + ("id" -> id.trim.toLong))
  }
}
